package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.BindingVipRequestFilter;
import com.mmlc.network.filter.GetVCodeRequestFilter;
import com.mmlc.network.responsebean.CardInfoResponseBean;
import com.mmlc.network.responsebean.GetVCodeResponseBean;
import com.mmlc.utils.EditInputUtils;
import com.mmlc.utils.TimeCount;
import com.mmlc.utils.ToastUtils;

/**
 * Created by Administrator on 2017/5/22.
 */

public class BindingVipActivity extends InterActivity {
    private ImageView iv_back;
    private ImageView iv_set;
    private TextView tv_title;

    private EditText et_idCardNum;
    private EditText et_phoneNumber;
    private EditText et_verificationCode;
    private TextView tv_send;
    private Button btn_binding;

    private TimeCount timeCount;

    @Override
    protected int initContentView() {
        return R.layout.activity_binding_vip;
    }

    @Override
    protected void initView() {
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_set = (ImageView) findViewById(R.id.iv_set);
        tv_title = (TextView) findViewById(R.id.tv_title);

        et_idCardNum = (EditText) findViewById(R.id.et_idCardNum);
        et_phoneNumber = (EditText) findViewById(R.id.et_phoneNumber);
        et_verificationCode = (EditText) findViewById(R.id.et_verificationCode);

        tv_send = (TextView) findViewById(R.id.tv_send);
        btn_binding = (Button) findViewById(R.id.btn_binding);

        tv_title.setText("绑定VIP");
        iv_set.setVisibility(View.GONE);
    }

    @Override
    protected void initEvent() {
        iv_back.setOnClickListener(this);
        tv_send.setOnClickListener(this);
        btn_binding.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.tv_send:
                getVCode();
                break;
            case R.id.btn_binding:
                toBinding();
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    /**
     * 获取验证码
     */
    private void getVCode(){
        //校验电话号码是否合法
        boolean isPhone = EditInputUtils.checkedPhoneNumber(et_phoneNumber.getText().toString());
        if (!isPhone) {
            ToastUtils.showToast(this, "电话号码不正确");
            return;
        }

        timeCount = new TimeCount(60000,1000,tv_send);
        GetVCodeRequestFilter filter = new GetVCodeRequestFilter(this);
        filter.requestBean.phone = et_phoneNumber.getText().toString().intern();
        filter.requestBean.method = "sendVcode";
        filter.requestBean.type = 4;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<GetVCodeResponseBean>() {

            @Override
            public void onSuccess(GetVCodeResponseBean result) {
                ToastUtils.showToast(getApplicationContext(), "验证码发送成功");
                if(timeCount != null){
                    timeCount.start();
                }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(BindingVipActivity.this,serverError.mess);
            }
        });
    }

    /**
     * 绑定VIP
     */
    private void toBinding(){
        String card_num = et_idCardNum.getText().toString().intern();
        if(TextUtils.isEmpty(card_num)){
            ToastUtils.showToast(BindingVipActivity.this, "请输入卡号");
            return;
        }
        String phone = et_phoneNumber.getText().toString().intern();
        if(TextUtils.isEmpty(phone)){
            ToastUtils.showToast(BindingVipActivity.this, "请输入手机号");
            return;
        }
        String vcode = et_verificationCode.getText().toString().intern();
        if(TextUtils.isEmpty(vcode)){
            ToastUtils.showToast(BindingVipActivity.this, "请输入验证码");
            return;
        }

        BindingVipRequestFilter filter = new BindingVipRequestFilter(this);
        filter.requestBean.card_num = card_num;
        filter.requestBean.phone = phone;
        filter.requestBean.vcode = vcode;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<CardInfoResponseBean>() {
            @Override
            public void onSuccess(CardInfoResponseBean result) {
                if(result != null){
                    if(result.getCode() == 0){
                        Intent intent = new Intent(BindingVipActivity.this,MyVipActivity.class);
                        startActivity(intent);
                        BindingVipActivity.this.finish();
                    }
                }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(BindingVipActivity.this,serverError.mess);
            }
        });
    }
}
